package com.nf.health.app.inquiry.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.PatientUserAdapter;
import com.nf.health.app.db.DbManager;
import com.nf.health.app.inquiry.bean.PatientUser;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PatientListActivity extends BaseActivity {

    @Bind(R.id.listview)
    ListView listview;
    List<PatientUser> users = new ArrayList<>();
    PatientUserAdapter adapter;
    int year;

    int gender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_patient_list);

        ButterKnife.bind(this);

        ViewUtil.setText(this, R.id.titlebar_title_tv, "选择问诊人");




        String nick_name = PreferenceHelper.getString(PreferenceHelper.USERNICK, "");
        String sex = PreferenceHelper.getString(PreferenceHelper.USERSEX, "");
        String birthday = PreferenceHelper.getString(PreferenceHelper.BIRTHDAY, "");
        if (TextUtils.isEmpty(nick_name) || TextUtils.isEmpty(sex) || TextUtils.isEmpty(birthday)){
            //如果有信息为空,不默认显示问诊人
        }else{
            if(sex.equals("2")){
                gender = 2;
            }else{
                gender = 1;
            }
            PatientUser user = new PatientUser();
            user.setNick(nick_name);
            user.setGender(gender);
            user.setBirthday(birthday);
            users.add(user);
        }

        users.addAll(DbManager.getInstance().getPatientUsers());

        adapter = new PatientUserAdapter(this, users);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0){
                    ActivityUtils.showActivityForResult(PatientListActivity.this, AddPatientActivity.class, 100);
                }
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 100 && resultCode == RESULT_OK){
            users.clear();
            String nick_name = PreferenceHelper.getString(PreferenceHelper.USERNICK, "");
            String sex = PreferenceHelper.getString(PreferenceHelper.USERSEX, "");
            String birthday = PreferenceHelper.getString(PreferenceHelper.BIRTHDAY, "");
            if(sex.equals("2")){
                gender = 2;
            }else{
                gender = 1;
            }
            PatientUser user = new PatientUser();
            user.setNick(nick_name);
            user.setGender(gender);
            user.setBirthday(birthday);
            users.add(user);

            users.addAll(DbManager.getInstance().getPatientUsers());

            adapter.notifyDataSetChanged();
        }
    }
}
